#include "widget.h"
#include "./ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setMaximumSize(260,360);
    this->setMinimumSize(260,360);
    this->setWindowTitle("计算器");
    //固定计算机窗口大小
}

Widget::~Widget()
{
    delete ui;
}
//定义槽
void Widget::on_b1_clicked()
{
    expression +="1";
    ui->sr->setText(expression);
}


void Widget::on_b2_clicked()
{
    expression +="2";
    ui->sr->setText(expression);
}


void Widget::on_b3_clicked()
{
    expression +="3";
    ui->sr->setText(expression);
}


void Widget::on_b4_clicked()
{
    expression +="4";
    ui->sr->setText(expression);
}


void Widget::on_b5_clicked()
{
    expression +="5";
    ui->sr->setText(expression);
}


void Widget::on_b6_clicked()
{
    expression +="6";
    ui->sr->setText(expression);
}


void Widget::on_b7_clicked()
{
    expression +="7";
    ui->sr->setText(expression);
}


void Widget::on_b8_clicked()
{
    expression +="8";
    ui->sr->setText(expression);
}


void Widget::on_b9_clicked()
{
    expression +="9";
    ui->sr->setText(expression);
}


void Widget::on_b0_clicked()
{
    expression +="0";
    ui->sr->setText(expression);
}


void Widget::on_bleft_clicked()
{
    expression +="(";
    ui->sr->setText(expression);
}


void Widget::on_bright_clicked()
{
    expression +=")";
    ui->sr->setText(expression);
}


void Widget::on_bjia_clicked()
{
    expression +="+";
    ui->sr->setText(expression);
}


void Widget::on_bjian_clicked()
{
    expression +="-";
    ui->sr->setText(expression);
}


void Widget::on_bcheng_clicked()
{
    expression +="*";
    ui->sr->setText(expression);
}


void Widget::on_bchu_clicked()
{
    expression +="/";
    ui->sr->setText(expression);
}


void Widget::on_bhui_clicked()
{
    expression.chop(1);
    ui->sr->setText(expression);
}

void Widget::on_bc_clicked()
{
    expression.clear();
    ui->sr->clear();
}


void Widget::on_bmi_clicked()
{
    expression +="^";
    ui->sr->setText(expression);
}


void Widget::on_bsin_clicked()
{
    QString str = ui->sr->text();
    double num = str.toDouble();
    double result = sin(num * 3.1415926 / 180);
    ui->sr->setText(QString::number(result));
}


void Widget::on_bcos_clicked()
{
    QString str = ui->sr->text();
    double num = str.toDouble();
    double result = cos(num * 3.1415926 / 180);
    ui->sr->setText(QString::number(result));
}


void Widget::on_btan_clicked()
{
    QString str = ui->sr->text();
    double num = str.toDouble();
    double result = tan(num * 3.1415926 / 180);
    ui->sr->setText(QString::number(result));
}


void Widget::on_bln_clicked()
{
    QString str = ui->sr->text();
    double num = str.toDouble();
    double result = log(num);
    ui->sr->setText(QString::number(result));
}


void Widget::on_bsqr_clicked()
{
    QString str = ui->sr->text();
    double num = str.toDouble();
    double result = sqrt(num);
    ui->sr->setText(QString::number(result));

}
//数据处理
void Widget::on_bequal_clicked()
{
    expression +="=";
    ui->sr->setText(expression);
    QStack<double> s_num,s_opt;
    char opt[128]={0};
    int i=0;double tmp=0;double num1,num2;
    QByteArray barr;
    barr=expression.toLatin1();
    strcpy(opt,barr.data());

    while(opt[i]!='\0'||s_opt.empty()!=true)
    {
        if(opt[i]>='0'&&opt[i]<='9')
        {
            tmp=tmp*10+opt[i]-'0';
            i++;
            if(opt[i]<'0'||opt[i]>'9')
            {
                s_num.push(tmp);
                tmp=0;
            }
        }
        else
        {
            if(s_opt.empty()==true||Priority(opt[i])>Priority(s_opt.top())||(s_opt.top()=='('&&opt[i]!=')'))
            {
                s_opt.push(opt[i]);
                i++;
                continue;
            }
            if(s_opt.top()=='('&&opt[i]==')')
            {
                s_opt.pop();
                i++;
                continue;
            }
            if(Priority(opt[i])<=Priority(s_opt.top())||(opt[i]==')'&&s_opt.top()!='(')||(opt[i]=='\0'&&s_opt.empty()!=true))
            {
                char ch=s_opt.top();
                s_opt.pop();
                switch(ch)
                {
                case '+':
                    num1=s_num.top();
                    s_num.pop();
                    num2=s_num.top();
                    s_num.pop();
                    s_num.push(num1+num2);
                    break;
                case '-':
                    num1=s_num.top();
                    s_num.pop();
                    num2=s_num.top();
                    s_num.pop();
                    s_num.push(num2-num1);
                    break;
                case '*':
                    num1=s_num.top();
                    s_num.pop();
                    num2=s_num.top();
                    s_num.pop();
                    s_num.push(num1*num2);
                    break;
                case '/':
                    num1=s_num.top();
                    s_num.pop();
                    num2=s_num.top();
                    s_num.pop();
                    if(num1 == 0){
                        ui->sr->setText("除数不能为零！");
                        return;
                    }
                    s_num.push(num2/num1);
                    break;
                case '^':
                    num1 = s_num.top();
                    s_num.pop();
                    num2 = s_num.top();
                    s_num.pop();
                    s_num.push(pow(num2, num1));
                    break;
                }

            }
        }
    }
    ui->sr->setText(QString::number(s_num.top()));
}

int Widget::Priority(char ch)
{
    switch (ch)
    {
    case '^':
        return 3;
    case '(':
        return 2;
    case '*':
    case '/':
        return 1;
    case '+':
    case '-':
        return 0;
    default:
        return -1;
    }
}

//特殊值快捷键

void Widget::on_be_clicked()
{
    double e = exp(1);
    ui->sr->setText(QString::number(e));

}


void Widget::on_bpai_clicked()
{
    double pi = M_PI;
    ui->sr->setText(QString::number(pi));

}













